﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Resto.CostControl.WebControl.DWZAdapter;
using Enterprise.Framework.Core.Container;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.Core.Query.SupplierModule.QueryObject;
using Enterprise.Framework.DomainModel;
using Resto.CostControl.Core;
using Resto.CostControl.Core.Query.SupplierModule.ViewObject;
using Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject;
using Resto.CostControl.Core.Facade.SupplierModule.FacadeObject;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.WebControl.Service;

namespace Resto.CostControl.Web.Controllers
{
    public class SupplierContractController : Controller
    {
        public ActionResult Index(DWZPageInfo pageInfo)
        {
            SupplierContractQO query = ObjectCreator.Create<SupplierContractQO>();
            PagerCondition pager = pageInfo.ToPager();

            ISpecification<SupplierContractVO> spec = null;
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = Specification<SupplierContractVO>.Create(p => p.Code == pageInfo.Keywords);
            }

            SupplierContractVO[] array = query.QuerySupplierContractVOByPager(spec, pager, "Code", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "Code";
            ViewBag.OrderDirection = "asc";

            return View(array);
        }

        public ActionResult Edit(string Id)
        {
            SupplierContractDTO data = null;
            if (Id != null)
            {
                SupplierContractFO facade = ObjectCreator.Create<SupplierContractFO>();

                data = facade.GetSupplierContractDTO(Id);
            }
            else
            {
                data = new SupplierContractDTO();
                data.Items = new SupplierContractItemDTO[0];
                data.BeginDate = DateTime.Now;
                data.EndDate = DateTime.Now;
            }
            return View(data);
        }

        [HttpPost]                                                                                
        public JsonResult Save(SupplierContractDTO data)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    List<SupplierContractItemDTO> list = new List<SupplierContractItemDTO>();
                    if (data.Items != null)
                    {
                        
                        foreach (var m in data.Items)
                        {
                            var item = new SupplierContractItemDTO
                            {
                                Price = m.Price,
                                MaterialId = m.MaterialId
                            };
                            list.Add(item);
                        }
                    }
                    data.Items = list.ToArray();

                    SupplierContractFO facade = ObjectCreator.Create<SupplierContractFO>();
                    if (!string.IsNullOrWhiteSpace(data.ID))
                        facade.UpdateSupplierContract(data);
                    else
                        facade.AddSupplierContract(data);
                }
                catch (BusinessValidationException ex)
                {
                    throw ex;
                }
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "SupplierContract",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }



        [HttpPost]
        public JsonResult Delete(string id)
        {
            SupplierContractFO facade = ObjectCreator.Create<SupplierContractFO>();
            facade.DeleteSupplierContract(id);

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "SupplierContract",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult Deletes(string[] Ids)
        {
            SupplierContractFO facade = ObjectCreator.Create<SupplierContractFO>();
            foreach (var id in Ids)
            {
                facade.DeleteSupplierContract(id);
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "SupplierContract",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "SupplierContract",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }
    }
}